package operator;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperator implements Operator{

    @Override
    public void work(BookList bookList) {
        System.out.println("-----新增图书-----");
        int p = bookList.getUsebook();
        if(p == bookList.getBooks().length) {
            System.out.println("书架已满，无法新增图书");
            return;
        }

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入书名：");
        String name = sc.nextLine();
        System.out.println("请输入作者：");
        String author = sc.nextLine();
        System.out.println("请输入类型：");
        String type = sc.nextLine();
        System.out.println("请输入价格：");
        double price = sc.nextDouble();

        for (int i = 0; i < p; i++) {
            if(name.equals(bookList.getBooks(i).getName())) {
                System.out.println("书架中已有该书");
                return;
            }
        }
        Book book1 = new Book(name, author, price, type);
        bookList.setBooks(book1, p);
        bookList.setUsebook(p + 1);
        System.out.println("新增图书成功");
    }
}
